<?php
namespace Admin\Controller;

use Think\Controller;

class OtherController extends Controller
{
//ajax不刷新上传
    public function upload()
    {
        if (is_dir('Uploads/Images/') == false) {
            mkdir('Uploads/Images/', '0777', true);
        }

        $upload           = new \Think\Upload();
        $upload->maxSize  = 3145728;
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Uploads/';
        $upload->savePath = '/Images/';
        $upload->replace  = true;
        $info             = $upload->upload();

        if (!$info) {
            $this->ajaxReturn($upload->getError());
        } else {
            $url   = '/Uploads' . $info['file']['savepath'] . $info['file']['savename'];
            $image = new \Think\Image();
            $image->open('./Uploads' . $info['file']['savepath'] . $info['file']['savename']);
            $width  = $image->width();
            $height = $image->height();
            if ($width >= 155 || $height >= 55) {
                $image->thumb(150, 50)->save('./Uploads' . $info['file']['savepath'] . $info['file']['savename']);
            }
            $rs = $this->upLogo($url);
            if ($rs) {
                $this->ajaxReturn($url);
            } else {
                $this->ajaxReturn('图片未加入数据库，请重试！');
            }

        }
    }

    //上传logo数据至数据库
    public function uplogo($url)
    {
        $rs = M('config')->where('id=1')->setField('logo', $url);
        return $rs;
    }

    //通用系统信息
    public function site($id)
    {
        $rs = F('siteinfo');
        if (!$rs) {
            $Config = M('config');
            $rs     = $Config->find(1);
            F('siteinfo', $rs);
        }

        if ($id == 1) {
            return $rs['site'];
        } elseif ($id == 2) {
            return $rs['keyword'];
        } elseif ($id == 3) {
            return $rs['description'];
        } elseif ($id == 4) {
            return $rs['code'];
        } elseif ($id  == 5) {
            return $rs['logo'];
        }

    }
}
